package com.tengju.user.infrastructure.repository.sign;

import com.tengju.user.domain.model.archive.sign.Sign;
import com.tengju.user.domain.model.archive.sign.SignId;
import com.tengju.user.domain.model.archive.sign.SignRepository;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.infrastructure.persistence.mybatis.dataobject.SignDO;
import com.tengju.user.infrastructure.persistence.mybatis.mapper.dealer.generate.SignDOMapper;
import com.tengju.user.infrastructure.repository.convert.SignConvert;
import org.springframework.stereotype.Repository;

@Repository
public class SignRepositoryImpl implements SignRepository {

    private final SignDOMapper signDOMapper;

    public SignRepositoryImpl(SignDOMapper signDOMapper) {
        this.signDOMapper = signDOMapper;
    }

    @Override
    public Sign find(UserIdCode idCode) {
        SignDO signDo = signDOMapper.selectByIdCode(idCode.getValue());
        return SignConvert.convert(signDo);
    }

    @Override
    public int update(SignId signId, Sign sign) {
        SignDO signDo = SignConvert.convert(signId, sign);
        return signDOMapper.updateByPrimaryKeySelective(signDo);
    }

    @Override
    public Long save(Sign sign) {
        SignDO signDo = SignConvert.convert(sign);
        signDOMapper.insertSelective(signDo);
        return signDo.getId();
    }
}
